home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Modules / environment.c < prev    next >
C/C++ Source or Header  |  1998-01-25  |  4KB  |  184 lines

  1. /*
  2. # Extended Environment module.
  3. #
  4. # Intended for use on the Amiga. Made by Irmen de Jong.
  5. #
  6. # This module contains all sorts of functions that operate on the global
  7. # or local environment of Python. Global environment = ENV: -vars,
  8. # local environment = the local shell variables.
  9. #
  10. #  11-Apr-96:    Rewrite. This module does not have to be posix compliant
  11. #                or portable, so...
  12. #  12-Jun-96:    Fixed zero-length var bugs. Renamed to new symbolnames.
  13. #  18-jan-98:    Updated for Python1.5
  14. */
  15.  
  16. #include "Python.h"
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. /* protos */
  21. static PyObject *put_environ Py_PROTO((PyObject *self, PyObject *args));
  22. static PyObject *get_environ Py_PROTO((PyObject *self, PyObject *args));
  23. static PyObject *set_environ Py_PROTO((PyObject *self, PyObject *args));
  24. static PyObject *unset_environ Py_PROTO((PyObject *self, PyObject *args));
  25. static PyObject *get_var Py_PROTO((PyObject *self, PyObject *args));
  26. static PyObject *set_var Py_PROTO((PyObject *self, PyObject *args));
  27. static PyObject *unset_var Py_PROTO((PyObject *self, PyObject *args));
  28.  
  29.  
  30. static struct PyMethodDef environment_methods[] = {
  31.         {"putenv", put_environ,1},
  32.         {"setenv", set_environ,1},
  33.         {"getenv", get_environ,1},
  34.         {"unsetenv", unset_environ,1},
  35. #ifdef _AMIGA
  36.         {"setvar", set_var,1},
  37.         {"getvar", get_var,1},
  38.         {"unsetvar", unset_var,1},
  39. #endif
  40.         {NULL, NULL}
  41. };
  42.  
  43.  
  44. void initenvironment Py_PROTO((void))
  45. {
  46.     (void)Py_InitModule("environment", environment_methods);
  47. }
  48.    
  49. /*
  50. **  putenv("name=value")    -- this is putenv(3)
  51. */
  52. static PyObject *put_environ(PyObject *self, PyObject *args)
  53. {
  54.     char *string;
  55.     if(PyArg_ParseTuple(args,"s",&string))
  56.     {
  57.         if ( putenv( string ) )
  58.         {
  59.             PyErr_SetString(PyExc_SystemError, "Error in system putenv call");
  60.             return NULL;
  61.         }
  62.         Py_INCREF(Py_None); return Py_None;
  63.     }
  64.     return NULL;
  65. }
  66.  
  67. /*
  68. ** setenv("name","value",overwrite?)
  69. */
  70. static PyObject *set_environ(PyObject *self, PyObject *args)
  71. {
  72.     char *name, *value;
  73.     int overwrite;
  74.  
  75.     if(PyArg_ParseTuple(args,"ssi",&name,&value,&overwrite))
  76.     {
  77.         if ( setenv(name,value,overwrite) )
  78.         {
  79.             PyErr_SetString(PyExc_SystemError, "Error in system setenv call");
  80.             return NULL;
  81.         }
  82.         Py_INCREF(Py_None); return Py_None;
  83.     }
  84.     return NULL;
  85. }
  86.  
  87. /*
  88. ** value = getenv("name")
  89. */
  90. static PyObject *get_environ(PyObject *self, PyObject *args)
  91. {
  92.     char *name, *val;
  93.     if(PyArg_ParseTuple(args,"s",&name))
  94.     {
  95.         if(val=getenv(name))
  96.         {
  97.             PyObject *s = PyString_FromString(val);
  98.             free(val);
  99.             return s; /* ok if s=NULL */
  100.         }
  101.         Py_INCREF(Py_None); return Py_None; /* var not found in env. */
  102.     }
  103.     return NULL;
  104. }
  105.  
  106. /*
  107. ** unsetenv("name")
  108. */
  109. static PyObject *unset_environ(PyObject *self, PyObject *args)
  110. {
  111.     char *string;
  112.     if(PyArg_ParseTuple(args,"s",&string))
  113.     {
  114.         unsetenv(string);
  115.         Py_INCREF(Py_None); return Py_None;
  116.     }
  117.     return NULL;
  118. }
  119.  
  120. #ifdef _AMIGA
  121. #include <proto/dos.h>
  122. #include <dos/var.h>
  123.  
  124. /*
  125. ** setvar("name","value",overwrite?)
  126. */
  127. static PyObject *set_var(PyObject *self, PyObject *args)
  128. {
  129.     char *name, *value;
  130.     int overwrite;
  131.  
  132.     if(PyArg_ParseTuple(args,"ssi",&name,&value,&overwrite))
  133.     {
  134.         if(!overwrite && FindVar(name,GVF_LOCAL_ONLY))
  135.         {
  136.             Py_INCREF(Py_None); return Py_None;
  137.         }
  138.  
  139.         if(SetVar(name,value,-1,GVF_LOCAL_ONLY))
  140.         {
  141.             Py_INCREF(Py_None); return Py_None;
  142.         }        
  143.         PyErr_SetString(PyExc_SystemError, "Error in dos SetVar call");
  144.         return NULL;
  145.     }
  146.     return NULL;
  147. }
  148.  
  149. /*
  150. ** value = getvar("name")
  151. */
  152. static PyObject *get_var(PyObject *self, PyObject *args)
  153. {
  154.     char *name;
  155.     char buf[200];
  156.  
  157.     if(PyArg_ParseTuple(args,"s",&name))
  158.     {
  159.         if(GetVar(name,buf,200,GVF_LOCAL_ONLY)>=0)
  160.         {
  161.             PyObject *s = PyString_FromString(buf);
  162.             return s; /* ok if s=NULL */
  163.         }
  164.         Py_INCREF(Py_None); return Py_None; /* var not found */
  165.     }
  166.     return NULL;
  167. }
  168.  
  169. /*
  170. ** unsetvar("name")
  171. */
  172. static PyObject *unset_var(PyObject *self, PyObject *args)
  173. {
  174.     char *string;
  175.     if(PyArg_ParseTuple(args,"s",&string))
  176.     {
  177.         DeleteVar(string,GVF_LOCAL_ONLY);
  178.         Py_INCREF(Py_None); return Py_None;
  179.     }
  180.     return NULL;
  181. }
  182.  
  183. #endif
  184.